import { queryInspectionList, actionInspectionAdd,actionInspectionEdit,queryInspectionInfo, actionInspectionExport,
  queryInspectionProcess,queryInspectionExamine,actionInspectionStatusExamine
} from '../services/inspection';
import { actionRepairStatusExamine } from '../services/repair';

export default {
  namespace: 'inspection',

  state: {
    processData:[],
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo :true,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_inspection_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryInspectionList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },


    // // 批量删除操作、中止
    // *action_plan_do({ payload }, { call, put }) {
    //   const response = yield call(actionPlanDo, payload);
    // },

    // 导出
    *action_inspection_export({ payload }, { call, put }) {
      const response = yield call(actionInspectionExport, payload);
    },


    // 新增
    *add_inspection({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionInspectionAdd,payload);
      response && (
        yield put({
          type: 'path/fetch_curing_clear',
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 修改
    *edit_inspection({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionInspectionEdit,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 详情
    *fetch_inspection_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryInspectionInfo, payload);
      response && (
        yield put({
          type: 'appendDataDetail',
          payload:{
            ...response.data,
            inspectPhoto:response.inspectPhoto,
          } ,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },

    // 稽查过程
    *fetch_inspection_handle({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryInspectionProcess, payload);
      response && (yield put({
        type: 'appendProcessData',
        payload:response.data.list,
      }));
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },

    // 稽查审核
    *fetch_inspection_examine({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(queryInspectionExamine, payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 审核状态改变
    *fetch_inspection_status_examine({ payload }, { call, put }) {
      const response = yield call(actionInspectionStatusExamine, payload);
    },


  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },

    appendProcessData(state, action) {
      return {
        ...state,
        processData: action.payload,
      };
    },

    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        processData:[],
        loading:{}
      };
    },
  },

};
